package com.happy.controller;

import com.happy.entity.FileEntity;
import com.happy.entity.NoteEntity;
import com.happy.service.IFileService;
import com.happy.service.INoteService;
import com.happy.util.CommonUtil;
import com.happy.util.ConstUtil;
import com.happy.util.FileUtil;
import com.happy.util.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 16:16 2021/1/23
 * @return
 */
@RestController
@RequestMapping ("/note")
@CrossOrigin (value = "*", maxAge = 3600)
@Slf4j
public class NoteController {


    @Resource
    private INoteService iNoteService;
    @Resource
    private IFileService iFileService;


    @RequestMapping ("/queryList")
    public ResultBody<NoteEntity> noteList (NoteEntity noteEntity) {
        List<NoteEntity> noteEntities = iNoteService.queryList ( noteEntity );
        return ResultBody.success ( noteEntities, noteEntities.size () );
    }


    /**
     * 添加成功
     * @param noteEntity
     * @return
     */
    @RequestMapping ("addSingle")
    public ResultBody<NoteEntity> addSingle (@RequestBody NoteEntity noteEntity) {
        noteEntity = iNoteService.addSingle ( noteEntity );
        return ResultBody.success ( noteEntity );
    }


    /**
     * 上传笔记内容里面的图片
     * @param file
     * @return
     */
    @RequestMapping ("upload")
    public ResultBody<NoteEntity> uploadImg (@RequestBody MultipartFile file) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern ( ConstUtil.DATE_FORMAT );
        StringBuffer sb = new StringBuffer ();  // 图片url地址
        String fileName = FileUtil.fileNameTreated ( file.getOriginalFilename () ); // 图片名称
        sb.append ( ConstUtil.NET_DOMAIN ).append ( "/" ).append ( dateTimeFormatter.format ( LocalDate.now () ) ).append ( "/" ).append ( fileName );
        String uploadDir = ConstUtil.DISK_DIRECT + CommonUtil.getFormatDateTime ( ConstUtil.DATE_FORMAT ); //D:upload/2021-02-08/
        try {
            FileUtil.upload ( file, uploadDir, fileName );
        } catch ( Exception e ) {
            log.error ( "上传文件异常-{}", e.getMessage () );
        }
        return ResultBody.successMarkdown ( "1", "上传成功", sb.toString () );
    }


    /**
     * 上传笔记封面图片
     * 添加到FileEntity表并返回成功URL
     * @param file
     * @return
     */
    @RequestMapping ("uploadCover")
    public ResultBody<FileEntity> uploadCover (@RequestBody MultipartFile file, String noteId) {
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setType ( ConstUtil.FILE_TYPE_NOTE_COVER );
        fileEntity.setRelationId ( Integer.parseInt ( noteId ) );
        fileEntity = iFileService.addSingle ( fileEntity, file );
        return ResultBody.success ( fileEntity );
    }


}
